package com.wanban.downloader.utils

import java.util.regex.Pattern

object Constants {

    const val TIME_OUT = 30 * 1000L
    const val MAX_RETRY_TIME = 3
    const val MAX_ERROR_TS_COUNT = 10
    // EXT TAG
    const val TAG_PREFIX = "#EXT"
    const val TAG_KEY = "#EXT-X-KEY"
    const val TAG_ENDLIST = "#EXT-X-ENDLIST"
    const val TAG_VERSION = "#EXT-X-VERSION"
    const val TAG_MEDIA_DURATION = "#EXTINF"
    const val TAG_PLAYLIST_HEADER = "#EXTM3U"
    const val TAG_INIT_SEGMENT = "#EXT-X-MAP"
    const val TAG_STREAM_INF = "#EXT-X-STREAM-INF"
    const val TAG_DISCONTINUITY = "#EXT-X-DISCONTINUITY"
    const val TAG_MEDIA_SEQUENCE = "#EXT-X-MEDIA-SEQUENCE"
    const val TAG_TARGET_DURATION = "#EXT-X-TARGETDURATION"
    const val TAG_I_FRAME_STREAM_INF = "#EXT-X-I-FRAME-STREAM-INF"

    const val URI = "URI"
    const val M3U8 = "m3u8"
    const val VIDEO = "VIDEO"
    const val AUDIO = "AUDIO"
    const val CODECS = "CODECS"
    const val RESOLUTION = "RESOLUTION"
    const val FRAME_RATE = "FRAME-RATE"
    const val BANDWIDTH = "BANDWIDTH"
    const val BANDWIDTH_PRE = "BANDWIDTH="
    const val AVERAGE_BANDWIDTH = "AVERAGE-BANDWIDTH"
    const val TAG_STREAM_INF_PRE = "$TAG_STREAM_INF:"
    const val SP_KEY_IS_ONLY_WIFI_DOWNLOAD = "isOnlyWifi"


    const val METHOD_NONE = "NONE"
    const val METHOD_AES_128 = "AES-128"
    const val KEYFORMAT_IDENTITY = "identity"
    const val METHOD_SAMPLE_AES = "SAMPLE-AES"
    const val METHOD_SAMPLE_AES_CTR = "SAMPLE-AES-CTR"
    const val METHOD_SAMPLE_AES_CENC = "SAMPLE-AES-CENC"

    const val MIME_TYPE_M3U8_3 = "vnd.apple.mpegurl"
    const val MIME_TYPE_M3U8_2 = "application/x-mpegurl"
    const val MIME_TYPE_M3U8_4 = "applicationnd.apple.mpegurl"
    const val MIME_TYPE_M3U8_1 = "application/vnd.apple.mpegurl"
    const val MIME_TYPE_M3U8_5 = "application/octet-stream"

    const val LENGTH_UNSET: Long = -1
    const val MAX_REDIRECTS = 20
    const val MAX_BYTES_TO_DRAIN: Long = 2048
    const val HTTP_STATUS_TEMPORARY_REDIRECT = 307
    const val HTTP_STATUS_PERMANENT_REDIRECT = 308

    /**  默认读取超时，以毫秒为单位。  */
    const val DEFAULT_READ_TIMEOUT_MILLIS = 10 * 1000

    /** 默认链接超时，以毫秒为单位。  */
    const val DEFAULT_CONNECT_TIMEOUT_MILLIS = 10 * 1000

    /** Header  */
    const val RANGE = "Range"
    const val USER_AGENT = "User-Agent"
    const val CONTENT_RANGE = "Content-Range"
    const val CONTENT_LENGTH = "Content-Length"
    const val ACCEPT_ENCODING = "Accept-Encoding"
    const val CONTENT_TYPE = "Content-Type"




    // REGEX
    val REGEX_IV = Pattern.compile("IV=([^,.*]+)")
    val REGEX_URI = Pattern.compile("URI=\"(.+?)\"")
    val REGEX_KEYFORMAT = Pattern.compile("KEYFORMAT=\"(.+?)\"")
    val REGEX_VERSION = Pattern.compile("${TAG_VERSION}:(\\d+)\\b")

    val REGEX_MEDIA_SEQUENCE = Pattern.compile("${TAG_MEDIA_SEQUENCE}:(\\d+)\\b")
    val REGEX_TARGET_DURATION = Pattern.compile("${TAG_TARGET_DURATION}:(\\d+)\\b")
    val REGEX_ATTR_BYTERANGE = Pattern.compile("BYTERANGE=\"(\\d+(?:@\\d+)?)\\b\"")
    val REGEX_MEDIA_DURATION = Pattern.compile("${TAG_MEDIA_DURATION}:([\\d\\.]+)\\b")
    val REGEX_BANDWIDTH = Pattern.compile("${TAG_STREAM_INF}:.*(?:,\\s)?BANDWIDTH=(\\d+).*(?:RESOLUTION=([\\dx]+))?.*")
    val REGEX_METHOD = Pattern.compile(
        "METHOD=(" + METHOD_NONE + "|" + METHOD_AES_128 + "|" +
                METHOD_SAMPLE_AES + "|" + METHOD_SAMPLE_AES_CENC + "|" + METHOD_SAMPLE_AES_CTR + ")" + "\\s*(,|$)"
    )
    val CONTENT_RANGE_WITH_SIZE = Pattern.compile("bytes (?:(?:\\d+-\\d+)|\\*)/(\\d+)")
    val CONTENT_RANGE_WITH_START_AND_END = Pattern.compile("bytes (\\d+)-(\\d+)/(?:\\d+|\\*)")
}